﻿// 项目：	Guan.CMS.Web - Guan.CMS
// 文件名：	ArticleController.cs
// 
// 修改时间：2013-03-09 17:49
// 修改人：	Cooclsee

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using Guan.CMS.CommandProcessor.Dispatcher;
using Guan.CMS.Core.Common;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Domain.Commands.Article;
using Guan.CMS.Model.Entities;
using Guan.CMS.Web.Core.ActionFilters;
using Guan.CMS.Web.Core.Extensions;
using Guan.CMS.Web.Core.Models;
using Guan.CMS.Web.Helpers;
using Guan.CMS.Web.ViewModels.Article;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

namespace Guan.CMS.Web.Areas.Admin.Controllers
{
    [CmsAuthorize(Roles.Admin)]
    public class ArticleController : Controller
    {
        private readonly ICommandBus _commandBus;
        private readonly IArticleRepository _articleRepository;
        private readonly IArticleCategoryRepository _articleCategoryRepository;

        public ArticleController(ICommandBus commandBus, IArticleRepository articleRepository, IArticleCategoryRepository articleCategoryRepository)
        {
            _commandBus = commandBus;
            _articleRepository = articleRepository;
            _articleCategoryRepository = articleCategoryRepository;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            var vm = this.GetArticleList();
            return View(vm);
        }

        private IEnumerable<ArticleListViewModel> GetArticleList()
        {
            var articles = _articleRepository.GetList(true).ToList();
            var vm = Mapper.Map<IEnumerable<Article>, IEnumerable<ArticleListViewModel>>(articles);
            return vm;
        }

        [HttpPost]
        public ActionResult ListAjax([DataSourceRequest] DataSourceRequest request)
        {
            var vm = this.GetArticleList();
            return Json(vm.ToDataSourceResult(request));
        }

        public ActionResult Edit(int id)
        {
            var blogToEdit = _articleRepository.GetById(id);
            var formModel = Mapper.Map<Article, ArticleFormModel>(blogToEdit);
            var categories = _articleCategoryRepository.GetAll(false);
            ViewBag.ArticleCategory = categories.ToSelectListItems(id);
            return View(formModel);
        }

        [HttpPost, ActionName("Edit")]
        public ActionResult EditSave(ArticleFormModel form)
        {
            if (ModelState.IsValid)
            {
                var command = Mapper.Map<ArticleFormModel, AddOrUpdateArticleCommand>(form);
                command.Author = HttpContext.User.Identity.Name;
                IEnumerable<ValidationResult> errors = _commandBus.Validate(command);
                ModelState.AddModelErrors(errors);
                if (ModelState.IsValid)
                {
                    var result = _commandBus.Submit(command);
                    if (result.Success)
                    {
                        return RedirectToAction("Index");
                    }
                }
                var categories = _articleCategoryRepository.GetAll(false);
                ViewBag.ArticleCategory = categories.ToSelectListItems(form.CategoryId);
                if (form.Id == 0)
                {
                    return this.View("Create", form);
                }
                else
                {
                    return this.View("Edit", form);
                }
            }
            return RedirectToAction("List");
        }

        public ActionResult Create()
        {
            var form = new ArticleFormModel(){IsPublished = true};
            var categories = _articleCategoryRepository.GetAll(false);
            ViewBag.ArticleCategory = categories.ToSelectListItems(0);
            return View("Edit",form);
        }
    }
}